package com.example.messangersms;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class NewMessageActivity extends Activity {
        
	    EditText sender;
	    EditText mess;
	    String reci = " ";
	    String result = " ";
	    Context context;
	    
	   static final String HTTP_URL ="http://softeng.cs.uwosh.edu/students/cs342g5/addData.php?";
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_new_message);
		
	    sender = (EditText)findViewById(R.id.RecieverId);
		Button sendMes = (Button) findViewById(R.id.sendMessage);
	    mess = (EditText)findViewById(R.id.message);
	    
		sendMes.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				 if(sender.getText().toString().equals("")|| mess.getText().toString().equals(""))
				 {
					 Toast.makeText(getBaseContext(), "Please Insert Message/Reciever Info", Toast.LENGTH_SHORT).show();
				 }
				 else 
				 {
				    
					PostMessageTask task = new PostMessageTask();
					task.execute();
					returnResult();
				 }
			}
			
		});
	}
	
	private void returnResult() {
		while(result.equals("")){
			
		}
			context = getApplicationContext();
			Toast toast = Toast.makeText(context, result,
				Toast.LENGTH_LONG);
			toast.show();
			result = "";
	}	
	
	private class PostMessageTask extends AsyncTask<Void, Void, String> {

		@Override
		protected String doInBackground(Void... params) {
					
			
			HttpClient httpclient = new DefaultHttpClient();
			HttpPost httppost = new HttpPost(
					HTTP_URL);

			try {
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
						3);
				nameValuePairs.add(new BasicNameValuePair(
						"RecieverId", sender.getText().toString()));
				nameValuePairs.add(new BasicNameValuePair(
						" SenderId", LoginActivity.UserId));
				nameValuePairs.add(new BasicNameValuePair(
						"Message",mess.getText().toString()));
				httppost.setEntity(new UrlEncodedFormEntity(
						nameValuePairs));
				
				// Execute HTTP Post Request
		
				HttpResponse response = httpclient
						.execute(httppost);
				result = EntityUtils.toString(response
						.getEntity());
				System.out.println("This is the result: "+result);
				
			} catch (ClientProtocolException e) {

			} catch (IOException e) {

			}
			return result;
		}
	}
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.activity_new_message, menu);
		return true;
	}

}
